//
// Created by ASUS on 2024/6/13/星期四.
//
#include <bits/stdc++.h>
using namespace std;

long long findMaximumElegance(vector<vector<int>>& items, int k) {
	ranges::sort(items, greater<>());
	long long ans = 0, total = 0;
	unordered_set<int> vis;
	vector<int> dup;
	for (int i = 0; i < items.size(); i++) {
		if (i < k) {
			total += items[i][0];
			if (vis.count(items[i][1]) == 0) {
				vis.insert(items[i][1]);
			} else {
				dup.push_back(items[i][0]);
			}
		} else if (!dup.empty() && vis.count(items[i][1]) == 0) {
			vis.insert(items[i][1]);
			total -= dup.back();
			dup.pop_back();
			total += items[i][0];
		}
		ans = max(ans, total + (long long)vis.size() * (long long)vis.size());
	}
	return ans;
}